﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.MTHModels;

namespace thinger.MTHControlLib
{
    public partial class RecipeControl : UserControl
    {
        public RecipeControl()
        {
            InitializeComponent();
            //设置组件的样式 解决闪烁的问题，和背景问题
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);//此设置确保控件的所有绘制都通过 WM_PAINT 消息处理。这有助于避免绘制不一致和闪烁问题，
            this.SetStyle(ControlStyles.DoubleBuffer, true);//双缓冲是一种减少或消除闪烁的技术，它通过在内存中绘制控件的副本，然后再一次性将其绘制到屏幕上，从而提高了绘制的平滑性。
            this.SetStyle(ControlStyles.ResizeRedraw, true);//当控件大小改变时，此设置确保控件会重绘其内容。这有助于避免在调整控件大小时出现绘制问题，如未更新的区域或闪烁。
            this.SetStyle(ControlStyles.Selectable, true);//此设置使控件可选择。对于某些控件（如自定义控件），可能需要明确设置此标志以启用选择功能。
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); //此设置允许控件支持透明背景色。当控件的背景色设置为 Color.Transparent 时，它将显示其父容器的背景。
        }

        private string recipeTitle = "1#站点";

        [Browsable(true)]
        [Category("自定义属性")]
        [Description("设置标题")]
        public string RecipeTitle
        {
            get
            {
                return recipeTitle;
            }
            set
            {
                recipeTitle = value;
                this.title.TitleName = value;
                this.textSetExTempHigh.Title = $"{value}温度高限";
                this.textSetExTempLow.Title = $"{value}温度低限";
                this.textSetExHumidityHigh.Title = $"{value}湿度高限";
                this.textSetExHumidityLow.Title = $"{value}湿度低限";
            }
        }

        private Recipe recipeParm = new Recipe();
        [Browsable(false)]
        public Recipe RecipeParm
        {
            get {
                return GetRecipe(recipeParm);
            }
            set 
            {
                recipeParm = value;
                SetRecipe(recipeParm);


            }
        }

        private void SetRecipe(Recipe recipeParm)
        {
            this.textSetExTempHigh.CurrentValue = recipeParm.TempHigh;
            this.textSetExTempLow.CurrentValue = recipeParm.TempLow;
            this.textSetExHumidityHigh.CurrentValue = recipeParm.HumidityHigh;
            this.textSetExHumidityLow.CurrentValue = recipeParm.HumidityLow;
            this.checkTemp.Checked = recipeParm.TempAlarmEnable;
            this.checkHumidity.Checked = recipeParm.HumidityAlarmEnable;
        }

        private Recipe GetRecipe(Recipe recipeParm)
        {


            recipeParm.TempHigh = this.textSetExTempHigh.CurrentValue;
            recipeParm.TempLow = this.textSetExTempLow.CurrentValue;
            recipeParm.HumidityHigh = this.textSetExHumidityHigh.CurrentValue;
            recipeParm.HumidityLow = this.textSetExHumidityLow.CurrentValue;
            recipeParm.TempAlarmEnable = this.checkTemp.Checked;
            recipeParm.HumidityAlarmEnable = this.checkHumidity.Checked;

            return recipeParm;



        }
    }




}
